######################################################################
#
# wt::contextInfo package
#
######################################################################

package provide wt::contextInfo 1
package require wt

namespace eval ::wt::contextInfo:: {
}

proc ::wt::contextInfo::printContext {} {

  set items [list]

  set title "Query String Parameters"
  set table [queryParams table]
  lappend items [list $table $title 1]

  set title "Posted Parameters"
  set table [postParams table]
  lappend items [list $table $title 1]

  set title "Server Variables"
  set table [serverVars table]
  lappend items [list $table $title 0]

  set title "Request Cookies"
  set table [requestCookies table]
  lappend items [list $table $title 1]

  set title "Uploaded Files"
  set table [table create]
  foreach key [uploads keys] {
    set list [uploads getAll $key]
    foreach upload $list {
      table add $key [upload $upload table]
    }
  }
  lappend items [list $table $title 1]

  set title "Session"
  set table [session table]
  lappend items [list $table $title 1]

  set title "Request Headers"
  set table [requestHeaders table]
  lappend items [list $table $title 1]

  set title "Response Cookies"
  set table [table create]
  foreach key [responseCookies keys] {
    set list [responseCookies getAll $key]
    foreach cookie $list {
      table add $key [cookie $cookie table]
    }
  }
  lappend items [list $table $title 1]

  set title "Response Headers"
  set table [responseHeaders table]
  lappend items [list $table $title 1]

  set title "Error Headers"
  set table [errorHeaders table]
  lappend items [list $table $title 1]

  set title "Effective Settings"
  set table [settings table]
  lappend items [list $table $title 0]

  set title "Context Settings"
  set table [contextSettings table]
  lappend items [list $table $title 0]

  set title "Default Settings"
  set table [defaultSettings table]
  lappend items [list $table $title 0]

  set title "Base Settings"
  set table [baseSettings table]
  lappend items [list $table $title 0]

  set title "Script Stack"
  set table [table create]
  set scripts [wt::scripts]
  foreach {set i 0} {$i < [llength $script]} {incr i} {
    set script [lindex $scripts $i]
    set path [lindex $script 0]
    if {$path == ""} {
      set path "<string>"
    }
    set translation [lindex $script 1]
    table set $table "#$i \[$path\]" \
      "Translated size: [string length $translation]"
  }
  lappend items [list $table $title 0]

  set buf ""

  append buf "<h2>Wtcl Context</h2>\n"

  append buf "<table cellpadding=5 cellspacing=0 border=0 style=\"border: 1px solid black\">\n"

  for {set i 0} {$i < [llength $items]} {incr i} {
    foreach {table title multi} [lindex $items $i] {
      append buf [printContextItem $table $title $multi]
      if {$i < [llength $items] - 1} {
        append buf "<tr><td colspan=2>&nbsp;</td></tr>\n"
      }
    }
  }

  append buf "</table>\n"

  return $buf
}

proc ::wt::contextInfo::printContextItem {table title printMultipleValues} {
  set buf ""

  set size [table size $table]

  set countStr "("
  if {$size == 1} {
    append countStr "1 item"
  } else {
    append countStr "$size items"
  }
  append countStr ")"

  append buf "<tr bgcolor=black>"
  append buf "<td style=\"color: white;\"><b>[wt::htmlEncode $title]</b></td>"
  append buf "<td style=\"color: white;\"><b><i>[wt::htmlEncode $countStr]</i></b></td>"
  append buf "</tr>\n"

  foreach key [table keys $table] {
    append buf "<tr valign=top>"

    append buf "<td width=\"200\"><b>[wt::htmlEncode $key]</b></td>"

    append buf "<td width=\"400\">"

    if {$printMultipleValues} {
      set vals [table getAll $table $key]
      for {set i 0} {$i < [llength $vals]} {incr i} {
        set val [lindex $vals $i]
        append buf [printCellValue $val]
        if {$i < [llength $vals] - 1} {
          append buf <br>
        }
      }
    } else {
      set val [table get $table $key]
      append buf [printCellValue $val]
    }

    append buf </td>
    append buf "</tr>\n"
  }

  return $buf
}

proc ::wt::contextInfo::printCellValue {val} {
  if {$val != ""} {
    set len [string length $val]
    if {$len > 200} {
      set val [htmlEncode [string range $val 0 199]]
      append val " <i>\[Truncated from $len bytes.\]</i>"
    } else {
      set val [htmlEncode $val]
    }
    return $val
  } else {
    return "&nbsp;"
  }
}

proc ::wt::contextInfo::translatedScript {} {
  set scripts [wt::scripts]
  if {[llength $scripts]} {
    return [lindex [lindex $scripts end] 1]
  }
  return ""
}

proc ::wt::contextInfo::printTranslatedScript {} {
  set buf "<h3>Translated Script</h3>\n"
  append buf "<pre>\n"
  append buf [htmlEncode [translatedScript]]
  append "\n</pre>\n"
}
